src/ArtaxServiceBuilder/OperationGenerator.php
function addCreateRequestMethod() {
        $body = '$request = new \Amp\Artax\Request();'.PHP_EOL;
        $url = $this->operationDefinition->getURL();
        $body .= '$url = null;'.PHP_EOL;
        $body .= sprintf('$request->setMethod(\'%s\');'.PHP_EOL, $this->operationDefinition->getHttpMethod());


        
        $body .= ''.PHP_EOL;

        $first = true;
        $hasQueryParams = false;
        $hasFormBody = false;
        $hasJsonBody = false;
        $hasURIVariables = false;

        foreach ($this->operationDefinition->getParameters() as $operationParameter) {

            if ($operationParameter->getLocation() === 'postField' ||
                $operationParameter->getLocation() === 'postFile') {
                $hasFormBody = true;
            }
            if ($operationParameter->getLocation() === 'json') {
                $hasJsonBody = true;
            }

            if ($operationParameter->getLocation() === 'uri') {
                $hasURIVariables = true;
            }

            if ($operationParameter->getLocation() === 'query') {
                $hasQueryParams = true;
            }

            if ($operationParameter->getLocation() === 'absoluteURL') {
                //$body .= '$url = '
                //TODO - throw an error if both absoluteURL and uri variables are set
            }
        }


        if ($hasQueryParams) {
            $body .= '$queryParameters = [];'.PHP_EOL;
        }
        

        $apiParameters = $this->apiGenerator->getAPIParameters();
        foreach ($this->operationDefinition->getParameters() as $operationParameter) {

            foreach ($apiParameters as $apiParameter) {
                if ($apiParameter === $operationParameter->getName()) {

                    if ($first) {
                        $body .= '//Add parameters that are defined at the API level, not the'.PHP_EOL;
                        $body .= '//operation level'.PHP_EOL;
                        $first = false;
                    }

                    $translatedParam = ucfirst($this->apiGenerator->translateParameter($operationParameter->getName()));
                    //TODO - this is wrong...they should be stored in just $params, then copied to query params
                    //if they are actually sent in the query.
                    $body .= sprintf(
                        "\$queryParameters['%s'] = \$this->api->get%s();".PHP_EOL,
                        ucfirst($apiParameter),
                        $translatedParam
                    );
                    $hasQueryParams = true;
                }
            }
        }

        $body .= ''.PHP_EOL;


        if ($hasFormBody) {
            $body .= '$formBody = new \Artax\FormBody;'.PHP_EOL;
        }

        if ($hasJsonBody == true) {
            $body .= '$jsonParams = [];'.PHP_EOL;
        }
        
        //TODO - check for multiple body types, either here or better yet in
        //operation definition. i.e. cannot set json body and multi-part in same request

        foreach ($this->operationDefinition->getParameters() as $operationParameter) {
            if ($operationParameter->getIsOptional() == true) {
                $body .= sprintf(
                    'if (array_key_exists(\'%s\', $this->parameters) == true) {'.PHP_EOL,
                    $operationParameter->getName()
                );
                $indent = '    ';
            }
            else {
                $indent = '';
            }

            $body .= sprintf(
                '$value = $this->getFilteredParameter(\'%s\');'.PHP_EOL,
                $operationParameter->getName()
            );
            $closeSkipBlock = '';

            if ($operationParameter->getSkipIfNull() == true) {
                $body .= $indent.'if ($value != null) {'.PHP_EOL;
                $closeSkipBlock = $indent.'}'.PHP_EOL;
                $indent .= '    ';
            }

            $body .= $this->generateParameterSetBlock($indent, $operationParameter);
            $body .= $closeSkipBlock;

            if ($operationParameter->getIsOptional() == true) {
                $body .= '}'.PHP_EOL;
            }
        }

        $body .= PHP_EOL;
        $body .= '//Parameters are parsed and set, lets prepare the request'.PHP_EOL;
        
        if ($hasFormBody == true) {
            $body .= '$request->setBody($formBody);'.PHP_EOL;
        }

        if ($hasJsonBody == true) {
            $body .= 'if (count($jsonParams)) {'.PHP_EOL;
            $body .= '    $jsonBody = json_encode($jsonParams);'.PHP_EOL;
            $body .= '    $request->setHeader("Content-Type", "application/json");'.PHP_EOL;
            $body .= '    $request->setBody($jsonBody);'.PHP_EOL;
            $body .= '}'.PHP_EOL;
        }

        //Nothing else has set the URL, use the one defined
        $body .= 'if ($url == null) {'.PHP_EOL;
        $body .= sprintf('    $url = "%s";'.PHP_EOL, addslashes($url));
        $body .= '}'.PHP_EOL;

        if ($hasURIVariables) {
            $body .= '$uriTemplate = new \ArtaxServiceBuilder\Service\UriTemplate\UriTemplate();'.PHP_EOL;
            $body .= '$url = $uriTemplate->expand($url, $this->parameters);'.PHP_EOL;
        }
        
        if ($hasQueryParams) {
            $body .= 'if (count($queryParameters)) {'.PHP_EOL;
            $body .= '    $url = $url.\'?\'.http_build_query($queryParameters, \'\', \'&\', PHP_QUERY_RFC3986);'.PHP_EOL;
            $body .= '}'.PHP_EOL;
        }

    
        $body .= '$request->setUri($url);'.PHP_EOL;
        $body .= ''.PHP_EOL;
        $body .= 'return $request;'.PHP_EOL;


        $tags[] = new GenericTag('return', '\Amp\Artax\Request');

        $docBlock = new DocBlockGenerator(
            'Create an Amp\Artax\Request object from the operation.',
            null,
            $tags
        );

        $methodGenerator = new MethodGenerator('createRequest');
        $methodGenerator->setDocBlock($docBlock);
        $methodGenerator->setBody($body);
        $this->classGenerator->addMethodFromGenerator($methodGenerator);
    }
function addCreateRequestMethod() {
        $body = '$request = new \Amp\Artax\Request();'.PHP_EOL;
        $url = $this->operationDefinition->getURL();
        $body .= '$url = null;'.PHP_EOL;
        $body .= sprintf('$request->setMethod(\'%s\');'.PHP_EOL, $this->operationDefinition->getHttpMethod());


        
        $body .= ''.PHP_EOL;

        $first = true;
        $hasQueryParams = false;
        $hasFormBody = false;
        $hasJsonBody = false;
        $hasURIVariables = false;

        foreach ($this->operationDefinition->getParameters() as $operationParameter) {

            if ($operationParameter->getLocation() === 'postField' ||
                $operationParameter->getLocation() === 'postFile') {
                $hasFormBody = true;
            }
            if ($operationParameter->getLocation() === 'json') {
                $hasJsonBody = true;
            }

            if ($operationParameter->getLocation() === 'uri') {
                $hasURIVariables = true;
            }

            if ($operationParameter->getLocation() === 'query') {
                $hasQueryParams = true;
            }

            if ($operationParameter->getLocation() === 'absoluteURL') {
                //$body .= '$url = '
                //TODO - throw an error if both absoluteURL and uri variables are set
            }
        }


        if ($hasQueryParams) {
            $body .= '$queryParameters = [];'.PHP_EOL;
        }
        

        $apiParameters = $this->apiGenerator->getAPIParameters();
        foreach ($this->operationDefinition->getParameters() as $operationParameter) {

            foreach ($apiParameters as $apiParameter) {
                if ($apiParameter === $operationParameter->getName()) {

                    if ($first) {
                        $body .= '//Add parameters that are defined at the API level, not the'.PHP_EOL;
                        $body .= '//operation level'.PHP_EOL;
                        $first = false;
                    }

                    $translatedParam = ucfirst($this->apiGenerator->translateParameter($operationParameter->getName()));
                    //TODO - this is wrong...they should be stored in just $params, then copied to query params
                    //if they are actually sent in the query.
                    $body .= sprintf(
                        "\$queryParameters['%s'] = \$this->api->get%s();".PHP_EOL,
                        ucfirst($apiParameter),
                        $translatedParam
                    );
                    $hasQueryParams = true;
                }
            }
        }

        $body .= ''.PHP_EOL;


        if ($hasFormBody) {
            $body .= '$formBody = new \Artax\FormBody;'.PHP_EOL;
        }

        if ($hasJsonBody == true) {
            $body .= '$jsonParams = [];'.PHP_EOL;
        }
        
        //TODO - check for multiple body types, either here or better yet in
        //operation definition. i.e. cannot set json body and multi-part in same request

        foreach ($this->operationDefinition->getParameters() as $operationParameter) {
            if ($operationParameter->getIsOptional() == true) {
                $body .= sprintf(
                    'if (array_key_exists(\'%s\', $this->parameters) == true) {'.PHP_EOL,
                    $operationParameter->getName()
                );
                $indent = '    ';
            }
            else {
                $indent = '';
            }

            $body .= sprintf(
                '$value = $this->getFilteredParameter(\'%s\');'.PHP_EOL,
                $operationParameter->getName()
            );
            $closeSkipBlock = '';

            if ($operationParameter->getSkipIfNull() == true) {
                $body .= $indent.'if ($value != null) {'.PHP_EOL;
                $closeSkipBlock = $indent.'}'.PHP_EOL;
                $indent .= '    ';
            }

            $body .= $this->generateParameterSetBlock($indent, $operationParameter);
            $body .= $closeSkipBlock;

            if ($operationParameter->getIsOptional() == true) {
                $body .= '}'.PHP_EOL;
            }
        }

        $body .= PHP_EOL;
        $body .= '//Parameters are parsed and set, lets prepare the request'.PHP_EOL;
        
        if ($hasFormBody == true) {
            $body .= '$request->setBody($formBody);'.PHP_EOL;
        }

        if ($hasJsonBody == true) {
            $body .= 'if (count($jsonParams)) {'.PHP_EOL;
            $body .= '    $jsonBody = json_encode($jsonParams);'.PHP_EOL;
            $body .= '    $request->setHeader("Content-Type", "application/json");'.PHP_EOL;
            $body .= '    $request->setBody($jsonBody);'.PHP_EOL;
            $body .= '}'.PHP_EOL;
        }

        //Nothing else has set the URL, use the one defined
        $body .= 'if ($url == null) {'.PHP_EOL;
        $body .= sprintf('    $url = "%s";'.PHP_EOL, addslashes($url));
        $body .= '}'.PHP_EOL;

        if ($hasURIVariables) {
            $body .= '$uriTemplate = new \ArtaxServiceBuilder\Service\UriTemplate\UriTemplate();'.PHP_EOL;
            $body .= '$url = $uriTemplate->expand($url, $this->parameters);'.PHP_EOL;
        }
        
        if ($hasQueryParams) {
            $body .= 'if (count($queryParameters)) {'.PHP_EOL;
            $body .= '    $url = $url.\'?\'.http_build_query($queryParameters, \'\', \'&\', PHP_QUERY_RFC3986);'.PHP_EOL;
            $body .= '}'.PHP_EOL;
        }

    
        $body .= '$request->setUri($url);'.PHP_EOL;
        $body .= ''.PHP_EOL;
        $body .= 'return $request;'.PHP_EOL;


        $tags[] = new GenericTag('return', '\Amp\Artax\Request');

        $docBlock = new DocBlockGenerator(
            'Create an Amp\Artax\Request object from the operation.',
            null,
            $tags
        );

        $methodGenerator = new MethodGenerator('createRequest');
        $methodGenerator->setDocBlock($docBlock);
        $methodGenerator->setBody($body);
        $this->classGenerator->addMethodFromGenerator($methodGenerator);
    }
Generate the method that creates an Ammp\Artax\Request from the operation.

TODO - refactor this into chunks when it's a bit more stable
TODO - use \Artax\Uri